
#ifndef XDNS_SERVER_MANAGER_H
#define XDNS_SERVER_MANAGER_H

#include "xdns.h"
#include <set>
#include <vector>

class XdnsData;
class XdnsHeaderInfo;
class XdnsQuery;
class XdnsServerManagerListener;
class XdnsServerState;

class XdnsServerManager
{
public:
    XdnsServerManager();
    virtual ~XdnsServerManager();

    /*! Initialize a new XDNS server manager.

        @param states Vector of the DNS server states. This function takes
                      the ownership of the XdnsServerState pointers.
        @param serverManager On return the initialized server manager.
    */
    static XdnsError Init(std::vector<XdnsServerState*>& states,
                          XdnsServerManager** serverManager);

    static void Release(XdnsServerManager* serverManager);

    void RegisterListener(XdnsServerManagerListener& listener);
    void UnregisterListener(XdnsServerManagerListener& listener);

    /*! Send a DNS query.

        @param query Pointer to the query to send. This function takes the
                     ownership of this pointer if success. It cannot be NULL.
        @param queryId On return the query ID if success. It could be NULL if
                       user did not care about it.
        @return XE_Ok if success.
    */
    virtual XdnsError SendQuery(XdnsQuery* query,
                                long* queryId
                                ) = 0;

    /* See xdns.h for description. */
    virtual void CancelQuery(long queryId) = 0;
    virtual XdnsError GetProcessingFds(int* nfds,
                                       fd_set* readfds,
                                       fd_set* writefds
                                       ) = 0;
    virtual XdnsError Process(fd_set* readfds,
                              fd_set* writefds
                              ) = 0;

protected:
    void NotifyListeners(long queryId,
                         XdnsError error,
                         const XdnsData* data,
                         size_t startOffset,
                         const XdnsHeaderInfo* header);

    std::set<XdnsServerManagerListener*> m_listeners;
};

#endif  // XDNS_SERVER_MANAGER_H

